package com.utils;

/**
 * @Description: TODO QQ1667847363
 * @author: xiao kun tai
 * @date:2022/1/12 19:49
 */

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Desc: IK 分词器工具类
 */
public class KeywordUtil {
    //使用 IK 分词器对字符串进行分词
    public static List<String> splitKeyword(String keyWord) throws IOException {
        //创建集合
        ArrayList<String> resultList = new ArrayList<>();

        StringReader reader = new StringReader(keyWord);

        IKSegmenter ikSegmenter = new IKSegmenter(reader, false);

        while (true){

            Lexeme next = ikSegmenter.next();
            if (next !=null){
                String word = next.getLexemeText();
                resultList.add(word);
            }else {
                break;
            }

        }

        //返回结果数据
        return resultList;
    }

    public static void main(String[] args) throws IOException {

        System.out.println(splitKeyword("学习Flink实时数仓项目我觉得很难"));
    }
}